// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.dbmanager.ui;

import org.dengues.warehouse.i18n.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class ContentComposite extends Composite {

    private CTabFolder folder;

    private ObjectBrowserComposite objectBrowserComposite;

    private DataEditorComposite dataBrowserComposite;

    private SqlEditorComposite sqlEditorComposite;

    private final DatabaseManagerDialog sqlQueryDialog;

    /**
     * Qiang.Zhang.Adolf@gmail.com ContentComposite constructor comment.
     * 
     * @param parent
     * @param sqlQueryDialog
     */
    public ContentComposite(Composite parent, DatabaseManagerDialog sqlQueryDialog) {
        super(parent, SWT.BORDER);
        this.sqlQueryDialog = sqlQueryDialog;
        setBackground(parent.getBackground());
        GridLayout gridLayout = new GridLayout();
        gridLayout.marginBottom = 0;
        gridLayout.marginWidth = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginHeight = 0;

        setLayout(gridLayout);
        setLayoutData(new GridData(GridData.FILL_BOTH));

        initComposite();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "initComposite".
     */
    private void initComposite() {
        createTabFolder();
        createObjectBrowserTabItem();
        createDataBrowserTabItem();
        createSqlEditorTabItem();

        folder.setSelection(0);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createSqlEditorTabItem".
     */
    private void createSqlEditorTabItem() {
        sqlEditorComposite = new SqlEditorComposite(folder);
        sqlEditorComposite.setDialog(sqlQueryDialog);
        createTabItem(Messages.getString("SqlEditorComposite.Title.Name"), sqlEditorComposite); //$NON-NLS-1$
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createDataBrowserTabItem".
     */
    private void createDataBrowserTabItem() {
        dataBrowserComposite = new DataEditorComposite(folder);
        dataBrowserComposite.setDialog(sqlQueryDialog);
        createTabItem(Messages.getString("ContentComposite.data.editor"), dataBrowserComposite); //$NON-NLS-1$
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createTabFolder".
     */
    private void createTabFolder() {
        if (folder == null || folder.isDisposed()) {
            clearParent();
            folder = new CTabFolder(this, SWT.NULL);
            folder.setSimple(false);
            folder.setToolTipText(Messages.getString("RunResultsComposit.Title.Name")); //$NON-NLS-1$
            GridData gd = new GridData(GridData.FILL_BOTH);
            folder.setLayoutData(gd);
            layout();
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "clearParent".
     */
    private void clearParent() {
        Control[] children = this.getChildren();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                children[i].dispose();
            }
        }

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createObjectBrowserTabItem".
     */
    private void createObjectBrowserTabItem() {
        objectBrowserComposite = new ObjectBrowserComposite(folder);
        createTabItem(Messages.getString("ContentComposite.object.editor"), objectBrowserComposite); //$NON-NLS-1$
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createTabItem".
     * 
     * @param text
     * @param defaultComposite
     */
    private void createTabItem(String text, Composite defaultComposite) {
        CTabItem item = new CTabItem(folder, SWT.NULL);
        item.setText(text);
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        layout.marginLeft = 0;
        layout.marginTop = 0;
        layout.marginBottom = 0;

        layout.horizontalSpacing = 0;
        layout.verticalSpacing = 0;
        layout.marginWidth = 0;
        layout.marginHeight = 0;

        defaultComposite.setLayout(layout);
        defaultComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
        item.setControl(defaultComposite);
    }

    /**
     * Getter for objectBrowserComposite.
     * 
     * @return the objectBrowserComposite
     */
    public ObjectBrowserComposite getObjectBrowserComposite() {
        return this.objectBrowserComposite;
    }

    /**
     * Getter for dataBrowserComposite.
     * 
     * @return the dataBrowserComposite
     */
    public DataEditorComposite getDataBrowserComposite() {
        return this.dataBrowserComposite;
    }

    /**
     * Getter for sqlEditorComposite.
     * 
     * @return the sqlEditorComposite
     */
    public SqlEditorComposite getSqlEditorComposite() {
        return this.sqlEditorComposite;
    }
}
